package com.kyzh.core.activities

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import com.gushenge.core.base.activity.BaseActivity
import com.gushenge.core.beans.Address
import com.gushenge.core.beans.Code
import com.gushenge.core.beans.ProductDetail
import com.gushenge.core.dao.GlobalConsts
import com.gushenge.core.dao.GlobalKeys
import com.gushenge.core.dao.GlobalKeys.KEY
import com.gushenge.core.dao.MMKVConsts.uid
import com.kyzh.core.R
import com.kyzh.core.dialog.showExchangeDialog
import com.kyzh.core.pager.weal.address.AddressActivity
import com.kyzh.core.utils.*
import kotlinx.android.synthetic.main.act_orderdone.*
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import org.jetbrains.anko.toast
import rxhttp.toClass
import rxhttp.wrapper.param.RxHttp

class OrderDoneActivity : BaseActivity() {
    private var detail: ProductDetail? = null
    private var aid: String? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.act_orderdone)
        initView()
    }

    private fun initView() {
        detail = intent.getSerializableExtra(KEY) as ProductDetail
        initData()
        order_address_select.setOnClickListener { v: View? ->
            val intent = Intent(this, AddressActivity::class.java)
            intent.putExtra(GlobalKeys.SELECT, true)
            startActivityForResult(intent, 1)
        }
        order_get.setOnClickListener {

            if (detail!!.cat_id != "3") {
                if (aid.isNullOrBlank()) {
                    toast("请选择地址")
                    return@setOnClickListener
                }
            }
            showExchangeDialog(detail?.goods_name ?: "", detail?.market_price ?: "0") {
                GlobalScope.launch {
                    val result = RxHttp.postForm(GlobalConsts.URL_ORDER_COMMIT)
                        .add("uid", uid)
                        .add("address_id", aid)
                        .add("t", time)
                        .add("id", detail?.goods_id)
                        .add("num", "1")
                        .add("sign", md5("$uid${time}fsd213ewdsadqwe2121213edsad"))
                        .toClass<Code<String>>()
                        .await()

                    result.apply {
                        runOnUiThread {
                            toast(message)
                            if (code == 1) {
                                finish()
                            }
                        }
                    }
                }
            }
        }

    }

    private fun initData() {
        order_pname.text = detail?.goods_name
        ivImage.loadImage(detail?.goods_img)
        order_score.text = String.format(getString(R.string.order_price), detail?.market_price)
        order_num.text = "1"
        if (detail!!.cat_id == "3") {
            order_address_select.setVisibility(false)
        } else {
            order_address_select.setVisibility(true)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 1) {
            if (resultCode == Activity.RESULT_OK) {
                val address = data?.getSerializableExtra(GlobalKeys.SELECT) as Address
                aid = address.id
                order_address.text = address.uaddress
                order_name.text = address.consignee
                order_phone.text = address.mobile
                initData()
            }
        }
    }
}